package cz.muni.fi.pa165.hauntedhouses.service;

import cz.muni.fi.pa165.hauntedhouses.dao.AbilityDAO;
import cz.muni.fi.pa165.hauntedhouses.dto.AbilityDTO;
import cz.muni.fi.pa165.hauntedhouses.dto.AbilityType;
import cz.muni.fi.pa165.hauntedhouses.entity.Ability;
import static cz.muni.fi.pa165.hauntedhouses.util.DTOAndDAOMapper.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * A service class for Ability controlling transactional behaviour.
 * 
 * @author Petr Suchý (373947)
 */
@Service
@Transactional
public class AbilityServiceImpl implements AbilityService{
    
    @Autowired
    private AbilityDAO abilityDAO;

    public void setAbilityDAO(AbilityDAO dao) {
        this.abilityDAO = dao;
    }

    @Override
    public void create(AbilityDTO ability) {
        Ability ab = DTOAbilityToEntityAbility(ability);
        abilityDAO.create(ab);
        ability.setId(ab.getId());
    }

    @Override
    public void update(AbilityDTO ability) {
        Ability ab = DTOAbilityToEntityAbility(ability);
        abilityDAO.update(ab);
        ability = AbilityEntityToDTOAbility(ab);
    }

    @Override
    public void remove(AbilityDTO ability) {
        abilityDAO.remove(DTOAbilityToEntityAbility(ability));
    }

    @Override
    public AbilityDTO get(Long id) {
        return AbilityEntityToDTOAbility(abilityDAO.get(id));
    }

    @Override
    public List<AbilityDTO> findAll() {
        List<AbilityDTO> dtoList = new ArrayList<>();
        for(Ability ability: abilityDAO.findAll()){
            dtoList.add(AbilityEntityToDTOAbility(ability));
        }
        return Collections.unmodifiableList(dtoList);
    }

    @Override
    public List<AbilityDTO> findByName(String name) {
        List<AbilityDTO> dtoList = new ArrayList<>();
        for(Ability ability: abilityDAO.findByName(name)){
            dtoList.add(AbilityEntityToDTOAbility(ability));
        }
        return Collections.unmodifiableList(dtoList);
    }

    @Override
    public List<AbilityDTO> findByType(AbilityType at) {
        List<AbilityDTO> dtoList = new ArrayList<>();
        for(Ability ability: abilityDAO.findByType(at)){
            dtoList.add(AbilityEntityToDTOAbility(ability));
        }
        return Collections.unmodifiableList(dtoList);
    }
    
}
